package com.xxbing.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxbing.dto.EdocEntryDto;
import com.xxbing.mapper.EdocEntryMapper;
import com.xxbing.model.EdocEntry;
import com.xxbing.service.EdocEntryService;
@Service("EdocEntryService")
public class EdocEntryServiceImpl implements EdocEntryService{
	
	@Autowired
	private EdocEntryMapper edocEntryMapper;
	
	
	@Override
	public List<EdocEntry> selectAll() {
		
		return edocEntryMapper.selectAll();
	}
	@Override
	public List<EdocEntry> selectByCategoryid(int categoryId) {
		
		return edocEntryMapper.selectByCategoryid(categoryId);
	}
	@Override
	public int insertEdocEntry(EdocEntry edoc) {
		
		return edocEntryMapper.insertEdocEntry(edoc);
	}
	@Override
	public EdocEntry selectById(int id) {
		
		return edocEntryMapper.selectById(id);
	}
	@Override
	public int updateDoc(EdocEntry edoc) {
		return edocEntryMapper.updateDoc(edoc);
	}
	@Override
	public int deleteDoc(int id) {
		return edocEntryMapper.deleteDoc(id);
	}
	
	
	
	
	//首页分检查询
	@Override
	public PageInfo selectByAll() {
		PageHelper.startPage(1, 3);
		List<EdocEntry> l = edocEntryMapper.selectAll();
		PageInfo pageInfo = new PageInfo<>(l);
		pageInfo.setList(l);
		return pageInfo;
	}
	
	//分页查询
	@Override
	public PageInfo selectByPages(EdocEntryDto dto) {
		int pageNum = dto.getPageNum();
		int categoryId = dto.getCategoryId();
		int pageSize = dto.getPageSize();
		PageHelper.startPage(pageNum, pageSize);
		List<EdocEntry> l = null;
		if(categoryId < 0) {
			l = edocEntryMapper.selectAll();
			
		}else {
			l= edocEntryMapper.selectByCategoryid(categoryId);
			
		}
		PageInfo pageInfo = new PageInfo<>(l) ;
		pageInfo.getList();
		return pageInfo;
	}
	
	
	//搜索框查询
	@Override
	public PageInfo selectByCategoryId(int categoryId) {
		PageHelper.startPage(1, 3);
		List<EdocEntry> l = null;
		if(categoryId < 0) {
			l = edocEntryMapper.selectAll();
			
		}else {
			l= edocEntryMapper.selectByCategoryid(categoryId);
			
		}
		PageInfo pageInfo = new PageInfo<>(l) ;
		pageInfo.getList();
		return pageInfo;
	}


	@Override
	public PageInfo addDoc(EdocEntry edoc) {
		PageHelper.startPage(1, 3);
		int row = edocEntryMapper.insertEdocEntry(edoc);
//		EdocEntry ed = edocEntryMapper.addEdocEntry(edoc);
		List<EdocEntry> l = edocEntryMapper.selectByCategoryid(edoc.getCategoryId());
		
		PageInfo pageInfo = new PageInfo<>(l);
		pageInfo.setList(l);
		return pageInfo;
	}
	@Override
	public PageInfo update(EdocEntry edoc,Integer pageNum, Integer selectVal) {
		PageHelper.startPage(pageNum, 3);
		int row = edocEntryMapper.updateDoc(edoc);
		List<EdocEntry> l = null;
		if(selectVal == -1) {
			l = edocEntryMapper.selectAll();
		}
		if(selectVal == 1) {
			l = edocEntryMapper.selectByCategoryid(edoc.getCategoryId());
		}
		PageInfo pageInfo = new PageInfo<>(l);
		pageInfo.setList(l);
		return pageInfo;
	}
	@Override
	public PageInfo deleteById(int id, Integer pageNum, Integer selectVal,Integer categoryId) {
		PageHelper.startPage(pageNum, 3);
		
		int row = edocEntryMapper.deleteDoc(id);
		List<EdocEntry> l = null;
		if(selectVal == -1) {
			l = edocEntryMapper.selectAll();
		}
		if(selectVal == 1) {
			l = edocEntryMapper.selectByCategoryid(categoryId);
		}
		PageInfo pageInfo = new PageInfo<>(l);
		pageInfo.setList(l);
		return pageInfo;
	}

}
